package demoMod.nanaki.patches.events.exordium;

import basemod.ReflectionHacks;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireReturn;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.AbstractEvent;
import com.megacrit.cardcrawl.events.exordium.GoldenWing;
import com.megacrit.cardcrawl.localization.EventStrings;
import com.megacrit.cardcrawl.vfx.cardManip.PurgeCardEffect;
import demoMod.nanaki.NanakiMod;
import demoMod.nanaki.characters.NanakiCharacter;

public class GoldenWingPatch {
    public static final String ID = NanakiMod.makeID("Golden Wing");
    private static final EventStrings eventStrings;
    public static final String NAME;
    public static final String[] DESCRIPTIONS;
    public static final String[] OPTIONS;

    @SpirePatch(
            clz = GoldenWing.class,
            method = SpirePatch.CONSTRUCTOR
    )
    public static class PatchConstructor {
        public static void Postfix(GoldenWing event) {
            if (AbstractDungeon.player instanceof NanakiCharacter) {
                event.imageEventText.updateDialogOption(0, OPTIONS[0]);
            }
        }
    }

    @SpirePatch(
            clz = GoldenWing.class,
            method = "buttonEffect"
    )
    public static class PatchButtonEffect {
        public static SpireReturn<Void> Prefix(GoldenWing event, int buttonPressed) {
            if (AbstractDungeon.player instanceof NanakiCharacter) {
                try {
                    Enum screen = ReflectionHacks.getPrivate(event, GoldenWing.class, "screen");
                    switch (screen.name()) {
                        case "INTRO":
                            if (buttonPressed == 0) {
                                event.imageEventText.updateBodyText(DESCRIPTIONS[1]);
                                event.imageEventText.updateDialogOption(0, OPTIONS[7]);
                                event.imageEventText.removeDialogOption(1);
                                event.imageEventText.removeDialogOption(1);
                                ReflectionHacks.setPrivate(event, GoldenWing.class, "screen", Enum.valueOf((Class<Enum>) Class.forName("com.megacrit.cardcrawl.events.exordium.GoldenWing$CUR_SCREEN"), "MAP"));

                                CardGroup tmp = CardGroup.getGroupWithoutBottledCards(AbstractDungeon.player.masterDeck.getPurgeableCards());
                                for (int i=0;i<2;i++) {
                                    if (!tmp.isEmpty()) {
                                        AbstractCard card = tmp.getRandomCard(AbstractDungeon.miscRng);
                                        tmp.removeCard(card);
                                        AbstractDungeon.topLevelEffects.add(new PurgeCardEffect(card, (float)(Settings.WIDTH / 2), (float)(Settings.HEIGHT / 2)));
                                        AbstractEvent.logMetricCardRemovalAndDamage("Golden Wing", "Card Removal", card, 0);
                                        AbstractDungeon.player.masterDeck.removeCard(card);
                                    }
                                }
                                return SpireReturn.Return(null);
                            }
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            return SpireReturn.Continue();
        }
    }

    static {
        eventStrings = CardCrawlGame.languagePack.getEventString(ID);
        NAME = eventStrings.NAME;
        DESCRIPTIONS = eventStrings.DESCRIPTIONS;
        OPTIONS = eventStrings.OPTIONS;
    }
}
